import com.facebook.graph.FacebookMobile;

import flash.events.LocationChangeEvent;
import flash.geom.Rectangle;
import flash.media.StageWebView;

import mx.events.FlexEvent;
import mx.events.StateChangeEvent;
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;



private var appID:String = "138491886310077";
public var xmlStorage:XMLStorage;
public var facebookInfo:FacebookInfo;


private var loginWebView:StageWebView = new StageWebView();

protected function initProcess(event:FlexEvent):void
{
	// TODO Auto-generated method stub
	checkLogin();
}


private function checkLogin():void{
	//facebookMobile = new FacebookMobile();
	FacebookMobile.logout();
	FacebookMobile.init(appID, handleInit);
	
	// TODO Auto-generated method stub
	if(getPref()){
		passLogin();
	}else{
		goLogin();
	}
	
}

private function handleInit(session:Object, fail:Object):void{
	
}



private function getPref():Boolean{
	
	xmlStorage = new XMLStorage();
	facebookInfo = xmlStorage.getFacebookInfo();
	
	if(facebookInfo != null && facebookInfo.accessToken != null){
		if(facebookInfo.accessToken != ''){
			return true;
		}else{
			return false;
		}
	}else{
		return false;
	}
	
	return true;
}

private function passLogin():void{
	
	//view_loginView.visible = false;
	this.currentState = "AdView";
	
	actCheckRegistration(facebookInfo.facebook_uid);
}

private function actCheckRegistration(facebook_uid:String):void{
	httpserv_checkRegistration.url = "http://artesophy.com/adon/app_api/" + "checkRegistration.php";
	
	var params:Object = new Object();
	params.fid = facebook_uid;
	
	httpserv_checkRegistration.send(params);
	
}

protected function resultCheckRegistration(event:ResultEvent):void
{
	// TODO Auto-generated method stub
	if(event.result.toString() == 'true'){
		passRegistration();
	}else{
		registration();
	}
	
}

/** when all login process was done */
private function passRegistration():void{
	this.currentState = "Account";
	accountMain.setMessage("로그인되었습니다.");
	accountMain.viewProfile();
	
}

private function registration():void{
	// insert registration processes.
	httpserv_registration.url = "http://artesophy.com/adon/app_api/" + "registration.php";
	
	var params:Object = new Object();
	params.fid = facebookInfo.facebook_uid;
	
	httpserv_registration.send(params);
}

protected function faultCheckRegistration(event:FaultEvent):void
{
	// TODO Auto-generated method stub
	trace("fault of checkRegistration : " + event.fault.toString());
}

protected function resultRegistration(event:ResultEvent):void
{
	// TODO Auto-generated method stub
	
	
	passRegistration();
}

protected function faultRegistration(event:FaultEvent):void
{
	// TODO Auto-generated method stub
	trace("registration was failed");
}

private function goLogin():void{
	
	this.currentState = "Account";
	accountMain.setMessage("페이스북 계정으로 로그인하세요");
}

public function openLoginWebView():void{
	
	loginWebView.stage = this.stage;
	
	loginWebView.viewPort = new Rectangle( 0, (this.height*0.1), this.width, (this.height*0.8) );
	
	loginWebView.addEventListener(LocationChangeEvent.LOCATION_CHANGING, changingWebView);
	loginWebView.addEventListener(LocationChangeEvent.LOCATION_CHANGE, changedWebView);
	
	FacebookMobile.login(handleLogin, this.stage, accessPermissions, loginWebView);
	
}

private function changingWebView(event:LocationChangeEvent):void{
	grp_loginWebViewIndicator.visible = true;
	trace("on");
}
private function changedWebView(event:LocationChangeEvent):void{
	grp_loginWebViewIndicator.visible = false;
	trace("off");
}

private function handleLogin(session:Object, fail:Object):void {
	if (session != null) {
		//
		trace("got uid : " + session.uid);
		
		saveFacebookSession(session);
		
		accountMain.viewProfile();
		
	}else if(fail != null) {
		trace("failed : " + fail.toString());
	}
}

private function saveFacebookSession(session:Object):void{
	facebookInfo = new FacebookInfo();
	facebookInfo.setBySession(session);
	xmlStorage.saveByFacebookInfo(facebookInfo);
	
	passLogin();
}







